package cc.wanforme.st.server.base.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.wanforme.st.server.base.entity.AdminRole;
import cc.wanforme.st.server.base.service.IAdminRoleService;
import cc.wanforme.st.server.util.AssertCheck;
import cc.wanforme.st.server.vo.ResMessage;
import cc.wanforme.st.server.vo.base.AdminRoleSimpleVo;
import cc.wanforme.st.server.vo.base.AdminRoleVo;

/** 用户角色的接口
 * @author wanne
 * @date 2022-03-05
 */
@Controller  
@RequestMapping("/base/admin-role")
public class AdminRoleController {

	@Autowired
	private IAdminRoleService adminRoleService;
	
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('adminRole:add')")
	@ResponseBody
	public ResMessage<AdminRole> add(@RequestBody AdminRole vo) {
		AdminRole vo2 = adminRoleService.saveVo(vo);
		return ResMessage.success(vo2);
	} 
	
	@PostMapping("/update")
	@PreAuthorize("hasAuthority('adminRole:update')")
	@ResponseBody
	public ResMessage<Void> update(@RequestBody AdminRole vo) {
		adminRoleService.updateVo(vo);
		return ResMessage.success();
	}
	
	@PostMapping("/updateRoles")
	@PreAuthorize("hasAuthority('adminRole:update')")
	@ResponseBody
	public ResMessage<Void> updateRoles(@RequestBody AdminRoleVo vo) {
		adminRoleService.updateAdminRoles(vo);
		return ResMessage.success();
	}
	
	@PostMapping("/del/{id}")
	@PreAuthorize("hasAuthority('adminRole:delete')")
	@ResponseBody
	public ResMessage<Void> delete(@PathVariable String id) {
		adminRoleService.deleteVo(id);
		return ResMessage.success();
	}
	
//	/** 角色模糊搜索
//	 * @param queryVo
//	 * @return
//	 */
//	@PostMapping("/list")
//	@ResponseBody
//	public ResMessage<PageInfo<RolePermission>> list(@RequestBody SearchQueryVo<String> queryVo) {
//		PageInfo<RolePermission> result = rolePermissionService.list (queryVo.getPage(),
//				queryVo.getSize(), queryVo.getParam());
//		return ResMessage.success(result);
//	}
	
	@PostMapping("/roles")
	@ResponseBody
	public ResMessage<AdminRoleVo> roles(@RequestBody AdminRoleSimpleVo vo ) {
		AssertCheck.nonNull(vo.getAdminId(), "未知用户");
		AdminRoleVo roles = adminRoleService.selectAdminAndRoles(vo.getAdminId());
		return ResMessage.success(roles);
	}
	
}
